package com.jmu.content.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jmu.content.dto.ChargeStationPageDTO;
import com.jmu.content.mapper.ChargeStationMapper;
import com.jmu.content.pojo.ChargePile;
import com.jmu.content.pojo.ChargeStation;
import com.jmu.content.pojo.PriceList;
import com.jmu.content.result.PageResult;
import com.jmu.content.service.ChargePileService;
import com.jmu.content.service.ChargeStationService;
import com.jmu.content.service.PriceListService;
import com.jmu.content.vo.ChargeStationUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@Service
@Slf4j
public class ChargeStationServiceImpl extends ServiceImpl<ChargeStationMapper, ChargeStation> implements ChargeStationService {

    @Autowired
    PriceListService priceListService;
    @Lazy
    @Autowired
    ChargePileService chargePileService;


    @Override
    public PageResult pageQuery(ChargeStationPageDTO dto) {
        Page<ChargeStation> page = PageHelper.startPage(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<ChargeStation> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(dto.getName()!=null && dto.getName().length()!=0,ChargeStation::getName,dto.getName());
        queryWrapper.eq(dto.getStationId()!=null,ChargeStation::getId,dto.getStationId());
        queryWrapper.eq(dto.getStatus()!=null,ChargeStation::getStatus,dto.getStatus());
        List<ChargeStation> list = this.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return new PageResult(page.getTotal(),pageInfo.getList());
    }

    @Override
    public List<ChargeStationUserVO> listBatchPrice() {
        List<ChargeStation> list = this.list();

        List<ChargeStationUserVO> result = list.stream().map(item -> {
            LambdaQueryWrapper<ChargePile> pileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            pileLambdaQueryWrapper.eq(ChargePile::getStationId,item.getId());
            pileLambdaQueryWrapper.eq(ChargePile::getStatus,0);
            int size = chargePileService.count(pileLambdaQueryWrapper);
            ChargeStationUserVO vo = new ChargeStationUserVO();
            BeanUtils.copyProperties(item, vo);
            log.info(item.toString());
            LambdaQueryWrapper<PriceList> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PriceList::getStationId, item.getId())
                    .orderByAsc(PriceList::getPrice);
            List<PriceList> priceLists = priceListService.list(queryWrapper);
            if (priceLists == null || priceLists.size()==0){
                vo.setPrice(new BigDecimal(1.0));
            }else {
                vo.setPrice(priceLists.get(0).getPrice());
            }
            vo.setNumber(size);
            return vo;
        }).collect(Collectors.toList());
        return result;
    }
}
